/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.OrderTechnology;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.OrderTechnologyRepository;
import me.zhengjie.service.OrderTechnologyService;
import me.zhengjie.service.dto.OrderTechnologyDto;
import me.zhengjie.service.dto.OrderTechnologyQueryCriteria;
import me.zhengjie.service.mapstruct.OrderTechnologyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zcc
* @date 2021-07-15
**/
@Service
@RequiredArgsConstructor
public class OrderTechnologyServiceImpl implements OrderTechnologyService {

    private final OrderTechnologyRepository orderTechnologyRepository;
    private final OrderTechnologyMapper orderTechnologyMapper;

    @Override
    public Map<String,Object> queryAll(OrderTechnologyQueryCriteria criteria, Pageable pageable){
        Page<OrderTechnology> page = orderTechnologyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(orderTechnologyMapper::toDto));
    }

    @Override
    public List<OrderTechnologyDto> queryAll(OrderTechnologyQueryCriteria criteria){
        return orderTechnologyMapper.toDto(orderTechnologyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OrderTechnologyDto findById(String techId) {
        OrderTechnology orderTechnology = orderTechnologyRepository.findById(techId).orElseGet(OrderTechnology::new);
        ValidationUtil.isNull(orderTechnology.getTechId(),"OrderTechnology","techId",techId);
        return orderTechnologyMapper.toDto(orderTechnology);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderTechnologyDto create(OrderTechnology resources) {
        resources.setTechId(IdUtil.simpleUUID()); 
        return orderTechnologyMapper.toDto(orderTechnologyRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OrderTechnology resources) {
        OrderTechnology orderTechnology = orderTechnologyRepository.findById(resources.getTechId()).orElseGet(OrderTechnology::new);
        ValidationUtil.isNull( orderTechnology.getTechId(),"OrderTechnology","id",resources.getTechId());
        orderTechnology.copy(resources);
        orderTechnologyRepository.save(orderTechnology);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String techId : ids) {
            orderTechnologyRepository.deleteById(techId);
        }
    }

    @Override
    public void download(List<OrderTechnologyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderTechnologyDto orderTechnology : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" oid",  orderTechnology.getOid());
            map.put(" color",  orderTechnology.getColor());
            map.put(" tech1",  orderTechnology.getTech1());
            map.put(" tech2",  orderTechnology.getTech2());
            map.put(" tech3",  orderTechnology.getTech3());
            map.put(" tech4",  orderTechnology.getTech4());
            map.put(" tech5",  orderTechnology.getTech5());
            map.put(" tech6",  orderTechnology.getTech6());
            map.put(" tech7",  orderTechnology.getTech7());
            map.put(" tech8",  orderTechnology.getTech8());
            map.put(" tech9",  orderTechnology.getTech9());
            map.put(" tech10",  orderTechnology.getTech10());
            map.put(" tech11",  orderTechnology.getTech11());
            map.put(" tech12",  orderTechnology.getTech12());
            map.put(" tech13",  orderTechnology.getTech13());
            map.put(" tech14",  orderTechnology.getTech14());
            map.put(" tech15",  orderTechnology.getTech15());
            map.put(" tech16",  orderTechnology.getTech16());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}